<?php
require_once ('../config/path_config.php');
require("admin-header.php");
require_once (ROOTDIR.'lang/cn.php');
require_once (ROOTDIR.'common/db_utils.php');

if (!(isset($_SESSION['administrator']))){
	echo "<a href='../loginpage.php'>Please Login First!</a>";
	exit(1);
}?>
<?php
if(isset($_POST['do'])) {
    require_once("../include/check_post_key.php");
    if (isset($_POST['rjpid'])) {
        $rjpid = intval($_POST['rjpid']);
        $sql = "UPDATE `solution` SET `result`=1 WHERE `problem_id`=?";
        DbUtil::getInstance()->query($sql, $rjpid);
        $sql = "delete from `sim` WHERE `s_id` in (select solution_id from solution where `problem_id`=?)";
        DbUtil::getInstance()->query($sql, $rjpid);
        $url = "../status.php?problem_id=" . $rjpid;
        echo "Rejudged Problem " . $rjpid;
        echo "<script>location.href='$url';</script>";
    } else if (isset($_POST['rjsid'])) {
        $rjsid = intval($_POST['rjsid']);
        $sql = "delete from `sim` WHERE `s_id`=?";
        DbUtil::getInstance()->query($sql, $rjsid);
        $sql = "UPDATE `solution` SET `result`=1 WHERE `solution_id`=?";
        DbUtil::getInstance()->query($sql, $rjsid);
        $url = "../status.php?top=" . ($rjsid + 1);
        echo "Rejudged Runid " . $rjsid;
        echo "<script>location.href='$url';</script>";
    } else if (isset($_POST['rjcid'])) {
        $rjcid = intval($_POST['rjcid']);
        $sql = "UPDATE `solution` SET `result`=1 WHERE `contest_id`=?";
        DbUtil::getInstance()->query($sql, $rjcid);
        $url = "../status.php?cid=" . ($rjcid);
        echo "Rejudged Contest id :" . $rjcid;
        echo "<script>location.href='$url';</script>";
    }
    echo str_repeat(" ", 4096);
    flush();
}
?>
<div class="container">
<b>Rejudge</b>
	<ol>
	<li><?php echo $MSG_PROBLEM?>
	<form action='rejudge.php' method=post>
		<input type=input name='rjpid' placeholder="1001">	<input type='hidden' name='do' value='do'>
		<input type=submit value=submit>
		<?php require_once("../include/set_post_key.php");?>
	</form>
	<li><?php echo $MSG_SUBMIT?>
	<form action='rejudge.php' method=post>
		<input type=input name='rjsid' placeholder="1002">	<input type='hidden' name='do' value='do'>
		<input type=hidden name="postkey" value="<?php echo $_SESSION['postkey']?>">
		<input type=submit value=submit>
	</form>
	<li><?php echo $MSG_CONTEST?>
	<form action='rejudge.php' method=post>
		<input type=input name='rjcid' placeholder="1003">	<input type='hidden' name='do' value='do'>
		<input type=hidden name="postkey" value="<?php echo $_SESSION['postkey']?>">
		<input type=submit value=submit>
	</form>
</div>
